package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wgq
 * @date 2019-10-22 15:57
 * 统计分析 -- > 运营数据
 */
/*{
        "data":{
                "todayVisitsNumber":0,
                "reportDate":"2019‐04‐25",
                "todayNewMember":0,
                "thisWeekVisitsNumber":0,
                "thisMonthNewMember":2,
                "thisWeekNewMember":0,
                "totalMember":10,
                "thisMonthOrderNumber":2,
                "thisMonthVisitsNumber":0,
                "todayOrderNumber":0,
                "thisWeekOrderNumber":0,
                "hotSetmeal":[
                        {"proportion":0.4545,"name":"粉红珍爱(女)升级TM12项筛查体检套餐","setmeal_count":5},
                        {"proportion":0.1818,"name":"阳光爸妈升级肿瘤12项筛查体检套餐","setmeal_count":2},
                        {"proportion":0.1818,"name":"珍爱高端升级肿瘤12项筛查","setmeal_count":2},
                        {"proportion":0.0909,"name":"孕前检查套餐","setmeal_count":1}
                 ],
            },
        "flag":true,
        "message":"获取运营统计数据成功"
    }*/
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    /**
     * 运营数据获取
     * @return
     */
    @Override
    public Map getBusinessReportData() throws Exception {
        //获取当前日期 --> reportDate
        String reportDate = DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月第一条日期
        String firstDayForThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //获取今日新会员 --> todayNewMember
        Integer todayNewMember = memberDao.findMemberCountByDate(reportDate);
        //获取本月新增会员数量 --> thisMonthNewMember
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDayForThisMonth);
        //获取本周新增会员数量 --> thisWeekNewMember
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //获取总会员数 --> totalMember
        Integer totalMember = memberDao.findMemberTotalCount();

        // 获取今日到诊数 --> todayVisitsNumber
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(reportDate);
        //获取本周到诊数  --> thisWeekVisitsNumber
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //获取本月到诊数  --> thisMonthVisitsNumber
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDayForThisMonth);


        //获取今日预约数 -- > todayOrderNumber
        Integer todayOrderNumber = orderDao.findOrderCountByDate(reportDate);
        //获取本周预约数 -- > thisWeekOrderNumber
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //获取本月预约数 -- > thisMonthOrderNumber
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDayForThisMonth);

        //热门套餐（取前4）
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        Map map = new HashMap();
        map.put("reportDate",reportDate);
        map.put("todayNewMember",todayNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("totalMember",totalMember);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("hotSetmeal",hotSetmeal);
        return map;
    }
}
